/**
 * 商品分类
 * */


(function () {
})();


//分类列表
function getList(){
    $.ajax({
        type: apis.brandIndex.method,
        url: apis.brandIndex.url,
        data: { start: pages.start, limit: pages.limit },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#pageTotal').html(res.total);
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>';
                if (d[i].cover_type == 1) {
                    h += '<img src="' + d[i].cover + '" height="240"/>';
                } else {
                    h += '<video width="320" height="240" class="videoContent" controls poster="/admin/static/video.jpg"><source src="' + d[i].cover + '" type="video/mp4"/></video>';
                }
                h += '</td>';
                h += '<td>' + d[i].gname + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                //h += '<td>' + d[i].type_name + '</td>';
                h += '<td>' + d[i].sort + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="brand_show(\'详情\',\'brand-show.html\', ' + d[i].id + ');" style="text-decoration:none">';
                h += '详情<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="brand_edit(\'品牌编辑\',\'brand-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
                h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += ' &nbsp; ';
                if (d[i].is_show == 0) {
                    h += '<a href="javascript:;" onclick="show_yes(' + d[i].id + ');">';
                    h += '显示';
                    h += '</a>';
                } else {
                    h += '<a href="javascript:;" onclick="show_no(' + d[i].id + ');">';
                    h += '隐藏';
                    h += '</a>';
                }
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="brand_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
                h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
                h += '</a>';
                h += '<br>';
                h += '<a href="javascript:;" onclick="lay_sort(\'排序编辑\',\'brand-sort.html\', ' + d[i].id + ');">';
                h += '设置排序';
                h += '</a>';
                h += '<input type="hidden" id="sort' + d[i].sort + '" value="' + d[i].sort + '"/>';
                //h += '<a href="javascript:;" onclick="type_qian(' + d[i].id + ');">';
                //h += '类型靠前';
                //h += '</a>';
                //h += ' &nbsp; ';
                //h += '<a href="javascript:;" onclick="type_hou(' + d[i].id + ');">';
                //h += '类型靠后';
                //h += '</a>';
                //h += '<br>';
                //h += '<a href="javascript:;" onclick="sort_qian(' + d[i].id + ');">';
                //h += '排序靠前';
                //h += '</a>';
                //h += ' &nbsp; ';
                //h += '<a href="javascript:;" onclick="sort_hou(' + d[i].id + ');">';
                //h += '排序靠后';
                //h += '</a>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//显示
function show_yes(id) {
    var msg = '确认要 <b style="color: green; font-size: 20px;">显示</b> 吗？';
    layer.confirm(msg, function (index) {
        showApi({id: id, kg: 1});
    });
}
//隐藏
function show_no(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">显示</b> 吗？';
    layer.confirm(msg, function (index) {
        showApi({id: id, kg: 0});
    });
}
//显示接口
function showApi(e){
    $.ajax({
        type: apis.brandKg.method,
        url: apis.brandKg.url,
        data: e,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            layer.msg('操作成功!',{icon: 1,time: 1000});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


////类型靠前
//function type_qian(id) {
//    var msg = '确认要 <b style="color: green; font-size: 20px;">靠前</b> 吗？';
//    layer.confirm(msg, function (index) {
//        typeApi({id: id, oper: 1});
//    });
//}
////类型靠后
//function type_hou(id) {
//    var msg = '确认要 <b style="color: red; font-size: 20px;">靠后</b> 吗？';
//    layer.confirm(msg, function (index) {
//        typeApi({id: id, oper: 2});
//    });
//}
////类型接口
//function typeApi(e){
//    $.ajax({
//        type: apis.brandTypeSort.method,
//        url: apis.brandTypeSort.url,
//        data: e,
//        success: function (res) {
//            console.log(res);
//            if (res.code < 0) {
//                msgPop({msg: res.msg}); return
//            }
//            layer.msg('操作成功!',{icon: 1,time: 1000});
//            setTimeout(function(){
//                top.location.reload();
//            }, 500);
//        },
//        error: function () {
//            console.log('错误了！');
//        }
//    });
//}
//
//
////排序靠前
//function sort_qian(id) {
//    var msg = '确认要 <b style="color: green; font-size: 20px;">靠前</b> 吗？';
//    layer.confirm(msg, function (index) {
//        sortApi({id: id, oper: 1});
//    });
//}
////排序靠后
//function sort_hou(id) {
//    var msg = '确认要 <b style="color: red; font-size: 20px;">靠后</b> 吗？';
//    layer.confirm(msg, function (index) {
//        sortApi({id: id, oper: 2});
//    });
//}
////排序接口
//function sortApi(e){
//    $.ajax({
//        type: apis.brandSort.method,
//        url: apis.brandSort.url,
//        data: e,
//        success: function (res) {
//            console.log(res);
//            if (res.code < 0) {
//                msgPop({msg: res.msg}); return
//            }
//            layer.msg('操作成功!',{icon: 1,time: 1000});
//            setTimeout(function(){
//                top.location.reload();
//            }, 500);
//        },
//        error: function () {
//            console.log('错误了！');
//        }
//    });
//}


//切换封面类型
function setCoverType(v){
    var imgOut = $('#imgOut'), videoOut = $('#videoOut');
    if (v == 1) {
        imgOut.show(); videoOut.hide();
    } else {
        imgOut.hide(); videoOut.show();
    }
}


//抓图
function upload2Image() {
    var fileObj = $('#bigUpload')[0], reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //console.log(res.target.result);
        $.ajax({
            type: apis.imageUpload.method,
            url: apis.imageUpload.url,
            data: { img: res.target.result },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layer.msg(res.msg,{icon: 1,time: 1000});
                    return
                }
                showImg({type: type, domain: res.data.domain, img: res.data.img, w: res.data.w, h: res.data.h});
            },
            error: function () {
                console.log('错误啦！');
            }
        });
    }
}

//图片上传
function upload2Video(){
    var uploadFile = $("#big2Upload")[0].files[0];
    var formData = new FormData();
    formData.append('video', uploadFile);
    $.ajax({
        type: apis.videoUpload.method,
        url: apis.videoUpload.url,
        data: formData,
        async: false,
        processData: false,
        contentType: false,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layer.msg(res.msg,{icon: 1,time: 1000});
                return
            }
            showVideo(res);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//添加弹窗
function brand_add(title,url,id,w,h){
    layer_show(title,url,w,h);
}


//添加弹窗
function brand_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//详情弹窗
function brand_show(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//排序弹窗
function lay_sort(title,url,id,w,h){
    url += '?id=' + id;
    var sort = $('#sort' + id).val();
    url += '&sort=' + sort;
    layer_show(title,url,w,300);
}


//保存更新
function sortModify(){
    var formData = {
        id: id,
        sort: $('#sort').val(),
    };
    console.log(formData);
    //return
    $.ajax({
        type: apis.brandSortModify.method,
        url: apis.brandSortModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//显示图片
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).height(e.h);
}


//显示图片
function showVideo(e){
    var h = '', i;
    h += '<video src="/' + e.data.fpath + '" poster="" style="height: 100vh;" controls></video>';
    h += '<input type="hidden" id="img2" value="' + e.data.fpath + '"/>';
    $('#big2Show').html(h).height(e.h);
}


//保存更新
function save(){
    var cover_type = $('#cover_type').val();
    var imglink = $('#img').val();
    var videolink = $('#img2').val();
    console.log(cover_type, imglink, videolink);
    if (cover_type==1) {
        if (imglink == '') {
            layerMsgErr('封面图片必须的');
            return
        }
    } else if (cover_type==2) {
        if (videolink == '') {
            layerMsgErr('封面视频必须的');
            return
        }
    } else {
        layerMsgErr('封面类型错误');
        return
    }
    var formData = {
        id: id,
        type: $('#type').val(),
        cover_type: cover_type,
        cover: cover_type==1 ? imglink : videolink,
        gno: $('#gno').val(),
        vlink: $('#vlink').val(),
        detail: ue.getContent()
    };
    console.log(formData);
    //return
    $.ajax({
        type: apis.brandAdd.method,
        url: apis.brandAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var msg = id==0 ? '添加成功' : '修改成功';
            msgPop({msg: msg});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//获取一个
function getOne(){
    $.ajax({
        type: apis.brandOne.method,
        url: apis.brandOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            $('#load').html('');
            var d = res.data;
            var h = '';
            //上传类型
            h = '';
            if (d.cover_type == 1) {
                h += '<option value="1" selected>图片</option>';
                h += '<option value="2">视频</option>';
            } else {
                h += '<option value="1">图片</option>';
                h += '<option value="2" selected>视频</option>';
            }
            $('#cover_type').html(h);
            //上传表单
            if (d.cover_type == 1) {
                h = '';
                h += '<img src="' + d.domain + d.cover + '" style="height: 100vh;"/>';
                h += '<input type="hidden" id="img" value="' + d.cover + '"/>';
                //$('#bigShow').html(h).width(d.iw).height(d.ih).show();
                $('#bigShow').html(h);
                $('#imgOut').show();
            }
            if (d.cover_type == 2) {
                h = '';
                h += '<video src="' + d.domain + d.cover + '" style="height: 100vh;" controls></video>';
                h += '<input type="hidden" id="img" value="' + d.cover + '"/>';
                $('#big2Show').html(h);
                $('#videoOut').show();
            }
            $('#gno')[0].value = res.data.gno;
            if (res.data.vlink != '') {
                $('#vlink').html('<a href="' + res.data.vlink + '" target="_blank">点击播放该商品：' + res.data.vlink + '</a>');
            }
            if (view == 'show') {
                $('#detail').html(res.data.detail);
            } else {
                ue.setContent(res.data.detail);
            }
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//详情
function showOne(){
    $.ajax({
        type: apis.brandOne.method,
        url: apis.brandOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            $('#load').html('');
            var d = res.data;
            var h = '';
            //上传类型
            h = '';
            if (d.cover_type == 1) {
                h += '<img src="' + d.domain + d.cover + '" style="height: 100vh;"/>';
            } else {
                h += '<video src="' + d.domain + d.cover + '" style="height: 100vh;" controls></video>';
            }
            $('#cover').html(h);
            $('#gno')[0].value = res.data.gno;
            if (res.data.vlink != '') {
                $('#vlink').html('<a href="' + res.data.vlink + '" target="_blank">点击播放该商品：' + res.data.vlink + '</a>');
            }
            if (view == 'show') {
                $('#detail').html(res.data.detail);
            } else {
                ue.setContent(res.data.detail);
            }
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function brand_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        //$(obj).parents("tr").remove();
        //console.log(id, index);
        $.ajax({
            type: apis.brandDel.method,
            url: apis.brandDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg); return
                }
                layerMsgOk('删除成功!');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}